package com.itheima.exce;

import java.util.InputMismatchException;
import java.util.Scanner;

public class ExceptionDemo5 {
    public static void main(String[] args) {
        Student student;
        try {
            student = new Student("李四", 18);
        } catch (RuntimeException e) {
            System.out.println("Student 对象创建失败：" + e.getMessage());
            return;
        }
        System.out.println("请输入姓名");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        student.setName(name);

        while (true) {
            System.out.println("请输入年龄");
            try {
                int age = sc.nextInt();
                student.setAge(age);
                break;
            } catch (InputMismatchException e) {
                sc.next();
                System.out.println("请输入一个整数");
            } catch (AgeOutOfBoundsException e) {
                System.out.println(e.getMessage());
                System.out.println(e);
                System.out.println("请输入符合范围的年龄");
            }
        }
        System.out.println(student);
    }
}

